package com.pocketpets.service.petdata;


import java.util.List;


import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.pocketpets.R;


public class PetDataAdapter extends ArrayAdapter<PetData> {

	// ================================================================
	// Fields
	// ================================================================

	private LayoutInflater mInflater;

	// ================================================================
	// Constructors
	// ================================================================

	public PetDataAdapter(Context context, int resource,
			int textViewResourceId, List<PetData> objects) {
		super(context, resource, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
		mInflater = (LayoutInflater) context
				.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
	}

	// ================================================================
	// Methods from Super Classes / Interfaces
	// ================================================================

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		PetData petData = getItem(position);

		if (null == convertView) {
			convertView = mInflater.inflate(R.layout.list, null);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);
		}

		holder = (ViewHolder) convertView.getTag();
		TextView name = holder.getNameView();
		name.setText(petData.getName());
		TextView detail = holder.getDetailView();
		detail.setText(petData.getDetail());
		ImageView photo = holder.getPhotoView();
		photo.setImageResource(R.drawable.icon);

		return convertView;
	}

	// ================================================================
	// Inner Classes
	// ================================================================

	private class ViewHolder {
		private View mRow;
		private TextView mNameView = null;
		private TextView mDetailView = null;
		private ImageView mPhotoView = null;

		public ViewHolder(View view) {
			mRow = view;
		}

		public TextView getNameView() {
			if (null == mNameView)
				mNameView = (TextView) mRow.findViewById(R.id.name);
			return mNameView;
		}

		public TextView getDetailView() {
			if (null == mDetailView)
				mDetailView = (TextView) mRow.findViewById(R.id.detail);
			return mDetailView;
		}

		public ImageView getPhotoView() {
			if (null == mPhotoView)
				mPhotoView = (ImageView) mRow.findViewById(R.id.photo);
			return mPhotoView;
		}

	}

}
